/**
 * Created by yunpeng on 2015/4/16.
 */
var Table = require('../utils/Table').Table;
var data = require('./series_data');
var series_data = data.series_data;

var names = [];
for(var name in series_data){
    names.push(name);
}

names.forEach(function(name){
    console.log(name);
    //var name = series_data[name].name;//汽车品牌
    var series_infos = series_data[name];

    series_infos.forEach(function(series_info){
        new Table().getConnection(function(connection){
            connection.beginTransaction(function(err) {
                if (err) { //throw err;
                    callback(false);
                }
                connection.query('INSERT INTO carbrand SET ?',{carbrand:series_info.name} , function(err, result) {
                    if (err) {
                        connection.rollback(function() {
                            //throw err;
                            callback(false);
                        });
                    }
                    var seriess = series_info["series_info"];
                    seriess.forEach(function(series){
                        var carnum = {cbid:result.insertId,num:series.name,createtime:new Date()};

                        connection.query('INSERT INTO carnum SET ?', carnum, function(err, result) {
                            if (err) {
                                connection.rollback(function() {
                                    //throw err;
                                    callback(false);
                                });
                            }

                        });

                    });

                    connection.commit(function(err) {
                        if (err) {
                            connection.rollback(function() {
                     //throw err;
                     /*callback(false);*/
                            });
                        }else{
                            connection.release(); //release
                            console.log('当前~~~'+series_info.name);
                        }
                     });
                });
            });
        });
    });


});

